
"""
This should automate audio using blender's aud module
While buffering may help with sounds that are played repeatedly, it fills up the ram
this is the reason I am not using buffering
"""
"""

"""
import bge
import aud

def audFactory(strPath, strName, booReverse, fadein, delay):
    """
    creates a soundfactory in aud, needed to play sounds
    file name must have extention (.ogg, .mp3, .wav, ect.)
    """
    path = bge.logic.expandPath('//')
    # set file as blank, then use if statements to create path
    file = ""
    if(strPath == "" or strPath == "/"):
        file = strName
        
    else:
        if(strPath.endswith("/")):
            file = strPath+strName
        else:
            file = strPath+"/"+strName
    
    # set and return factory 
    final = path+file        
    factory = aud.Factory.file(final)
    # reverse works best for seekable files, either buffered or wavs (seems to work with them)
    if(booReverse):
        factory = factory.reverse()
    
    # fades sound in over time, because I cannot get the sound length there won't be a fade out option    
    factory = factory.fadein(0.0, fadein)
    # delay until sound starts
    factory = factory.delay(delay)
    # returns sound factory
    return factory
    


def playSound(factory, loops):
    """
    creates sound handle when stored in a variable, string porperty or bge.logic.globalDict
    it will play the first time it is created
    These modules will come in handy
     'loop_count', 'pause', 'pitch', 
    'resume', 'status', 'stop',  'volume', 
    """
    # create and return handle
    sound = aud.device().play(factory.loop(loops))
    return sound


def silentHandle(factory, loops):
    """
    creates sound handle when stored in a variable, string porperty or bge.logic.globalDict
    it will NOT play the first time it is created (but you will have to resume the handle)
    These modules will come in handy
     'loop_count', 'pause', 'pitch', 
    'resume', 'status', 'stop',  'volume', 
    """
    # create and return handle paused
    sound = aud.device().play(factory.loop(loops))
    sound.pause()
    return sound
    
    
def DvolSound(handle, minVol, maxVol, maxDis):
    """
    basic 3d sound function, uses distance for volume
    """
    own = bge.logic.getCurrentController().owner
    scene = bge.logic.getCurrentScene()
    dis = own.getDistanceTo(scene.active_camera)
    volRange = (maxVol - minVol)/maxDis
    volume = maxVol - (dis*volRange)
    if(volume < minVol):
        volume = minVol
    if(volume > maxVol):
        volume = maxVol
    #handle.volume_maximum = maxVol
    #handle.volume_minimum = minVol
    print(dir(handle))
    print(handle.status)
    handle.resume()
    lhandle.volume = volume
   

def truDsound(handle, minVol, maxVol, maxDis, loops):
    """
    true 3d sound function, may need modification.
    only works with mono sounds
    special thanks to Dberube4  for his example
    """
    # get relevant data
    scene = bge.logic.getCurrentScene()
    cam = scene.active_camera
    own = bge.logic.getCurrentController().owner
    #device = aud.device()
    #handle = device.play(factory.loop(loops))
    print(cam.worldPosition)
    # set the distance model
    aud.device().distance_model = aud.AUD_DISTANCE_MODEL_LINEAR_CLAMPED
    # set the listener's location
    aud.device().listener_location = cam.worldPosition
    # set the listener's orientation
    aud.device().listener_orientation = cam.worldOrientation.to_quaternion()
    
    #Set sound to "not relative"
    handle.relative = False
    
    #Set Sound source position & Orientation
    handle.location = own.worldPosition
    handle.orientation = own.worldOrientation.to_quaternion()
    
    #Setting other values to make the sound "fade away"
    handle.distance_maximum = maxDis
    handle.distance_reference= 0


#Testing#
sound = audFactory("","shot.wav", False, 0, 0) 
sound2 = audFactory("","level1-step1-evil.wav", False, 0, 0) 
#hansound = audHandle(sound)
audio = playSound(sound, 0)
audio2 = playSound(sound2, 1)
#print(audio.status)
truDsound(audio, .01, .5, 500.0, 0)


"""
'attenuation', 'cone_angle_inner', 'c
one_angle_outer', 'cone_volume_outer', 'distance_maximum', 'distance_reference',
 'keep', 'location', 'loop_count', 'orientation', 'pause', 'pitch', 'position',
'relative', 'resume', 'status', 'stop', 'velocity', 'volume', 'volume_maximum',
'volume_minimum'

"""